//
//  WBBaseViewController.m
//  TransWB
//
//  Created by 孝飞王 on 2016/12/27.
//  Copyright © 2016年 孝飞王. All rights reserved.
//

#import "WBBaseViewController.h"
#import "WBNetworkManager.h"
#import "WBAuthorControllerViewController.h"

@interface WBBaseViewController ()

@end

@implementation WBBaseViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpUI];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:LoginSuccess object:nil];
    
}

//登录成功
- (void)loginSuccess:(NSNotification*)notification{
    
    //注销左右两边的按钮
    self.navItem.leftBarButtonItem = nil;
    self.navItem.rightBarButtonItem = nil;
    
    //更新UI 将访客视图变为表格视图 重新设置view
    //在访问view的getter方法的时候如果view 为nil的话 会重新调用loadView
    self.view = nil;
    
    //注销通知 接到通知会直接注册两次的
    [[NSNotificationCenter defaultCenter] removeObserver:LoginSuccess];
}
- (void)dealloc
{
    //注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:LoginSuccess];
}

- (void)setUpUI{
    
    [self setUpNavigationBar];
    [WBNetworkManager shareManager].isLogin ? [self setUpNomalUI] : [self visitorViewUI];
    
}

/**
 访客视图下的 UI
 */
- (void)visitorViewUI{
    
    [self.view  insertSubview:self.visitorView belowSubview:self.navBar];
    
    
    //给按钮添加监听事件
    [self.visitorView.registBtn addTarget:self action:@selector(compauseRegester) forControlEvents:UIControlEventTouchUpInside];
    [self.visitorView.longinbtn addTarget:self action:@selector(compauseLogin) forControlEvents:UIControlEventTouchUpInside];
    
}

/**
 正常登陆下的界面
 */
- (void)setUpNomalUI{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    //添加表格视图
    [self setUpTabelView];
    [self.view insertSubview:self.baseTabaleView belowSubview:self.navBar];
    [self loadData];
}

#pragma mark - 登录注册监听的方法
- (void)compauseLogin{
    
//    //跳转登录页面
//    WBAuthorControllerViewController *author = [[WBAuthorControllerViewController alloc]init];
//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:author];
//    [self presentViewController:nav animated:YES completion:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:ShouldLoginSuccess object:nil];
    
    NSLog(@"我登录了啊 ");
}

- (void)compauseRegester{
    NSLog(@"我注册了啊 ");
}

#pragma mark - TableViewDelegate&&TablaeViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSourceArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        }
    
        cell.textLabel.text = [self.dataSourceArray[indexPath.row] text];
        return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    NSInteger section = tableView.numberOfSections - 1;
    
    if (row < 0 || section < 0)  return;
    
    NSInteger count = [self.baseTabaleView numberOfRowsInSection:section];
    
    if (row == count - 1 && isPull  == NO) {
        
        NSLog(@"开始上拉加载");
        isPull = YES;
        [self loadData];
    }
    
}


#pragma mark - LoadData
- (void)loadData{
    
    isPull = NO;
    [self.refreshControl endRefreshing];
}


#pragma mark - LazyLoad-- 自 定义bar
- (UINavigationBar *)navBar{
    
    if (_navBar == nil) {
        
        _navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 64)];
        
    }
    
    return _navBar;
}

- (void)setUpNavigationBar{
    self.navBar.barTintColor = [UIColor cz_colorWithHex:0xF6F6F6];
    self.navBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]};
    self.navBar.tintColor = [UIColor orangeColor];
    self.navBar.items = @[self.navItem];
    [self.view addSubview:self.navBar];
    
}


#pragma mark  - 自定义item
- (UINavigationItem *)navItem{
    
    if (_navItem==nil) {
        
        _navItem = [[UINavigationItem alloc]init];
    }
    return _navItem;
}

- (void)setTitle:(NSString *)title{
   self.navItem.title = title;
}

- (NSString *)title{
    return self.navItem.title;
}

#pragma mark - 创建表格视图
- (UITableView *)baseTabaleView{
    
    if (_baseTabaleView == nil) {
        
        _baseTabaleView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _baseTabaleView.delegate = self;
        _baseTabaleView.dataSource = self;
        }
    
    return _baseTabaleView;
}

- (void)setUpTabelView{
    self.baseTabaleView.tableFooterView = [UIView new];
    //上遮下挡的问题
    self.baseTabaleView.contentInset = UIEdgeInsetsMake(self.navBar.bounds.size.height, 0, self.tabBarController.tabBar.bounds.size.height, 0 );
    self.baseTabaleView.scrollIndicatorInsets = self.baseTabaleView.contentInset;
    [self.baseTabaleView addSubview:self.refreshControl];

}



#pragma mark - DataSouceArray
- (NSMutableArray *)dataSourceArray{
    
    if (_dataSourceArray == nil) {
        _dataSourceArray = [[NSMutableArray alloc]init];
    }
    return _dataSourceArray;
}

#pragma mark- 刷新控件
- (UIRefreshControl *)refreshControl{
    
    if (_refreshControl == nil) {
        
        _refreshControl = [[UIRefreshControl alloc]init];
        //添加监听方法
        [_refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    }
    return _refreshControl;
}

#pragma mark - 访客视图
- (WBVisitorView *)visitorView{
    
    if (_visitorView == nil) {
        _visitorView = [[WBVisitorView alloc]initWithFrame:self.view.bounds];
        _visitorView.dataDic = self.dataDic;
    }
    return _visitorView;
}




@end
